package com.ms.game.lobby.terminatedaccountlist;

import java.util.ArrayList;
import java.util.Date;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;

import com.ms.game.lobby.terminatedaccountlist.db.TerminatedAccountListModel;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.db.model.basic.BasicModel;
import com.xpec.c4.game.time.TimeConstants;
import com.xpec.c4.game.time.TimeSys;

public class TerminatedAccountListDAO {
	
	public static boolean saveOrUpdateModel(BasicModel model){
		
		Session session = HibernateManager.txBegin();
		
		session.saveOrUpdate(model);
		
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	public static boolean deleteModel(BasicModel model){
		
		Session session = HibernateManager.txBegin();
		
		session.delete(model);
		
		return HibernateManager.txCommitFailRollbackClearSession();
		
	}
	
	/**
	 * 新增
	 * @param playerName
	 * @param reason
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static boolean insertTerminatedAccountListModel(String playerName, String reason, long startTime, long endTime) {
		TerminatedAccountListModel model = new TerminatedAccountListModel();
		model.setPlayerName(playerName);
		model.setReason(reason);
		model.setStartTime(startTime);
		model.setEndTime(endTime);
		model.setStartDate(new Date(startTime * TimeConstants.MILLI_SECOND_PRE_SECOND));
		model.setModifyTime(TimeSys.currentTimeSec());
		return saveOrUpdateModel(model);
	}
	
	/**
	 * 刪除
	 * @param id
	 * @return
	 */
	public static boolean deleteTerminatedAccountListModel(int id) {
		Session session = HibernateManager.getSession();
		TerminatedAccountListModel model = (TerminatedAccountListModel)session.get(TerminatedAccountListModel.class, id);
		if (model != null) {
			session = HibernateManager.txBegin();
			session.delete(model);
			return HibernateManager.txCommitFailRollbackClearSession();
		}
		
		HibernateManager.endSession();
		return false;
	}
	
	/**
	 * 用ID讀取
	 * @param id
	 * @return
	 */
	public static TerminatedAccountListModel loadTerminatedAccountListModelByDbId(int id){
		
		Session session = HibernateManager.getSession();
		
		TerminatedAccountListModel model = (TerminatedAccountListModel) session.get(TerminatedAccountListModel.class, id);    
		
		HibernateManager.endSession();
		
		return model;
	}

	/**
	 * 讀全部的model
	 * @return
	 */
	public static ArrayList<TerminatedAccountListModel> loadAllModels() {
		
		ArrayList<TerminatedAccountListModel> models = new ArrayList<TerminatedAccountListModel>();
		
		Session session = HibernateManager.getSession();
		
		Query query = session.createQuery("From TerminatedAccountListModel");
		query.setFetchSize(1000);
		ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
		while (results.next()) {
			Object[] rows = results.get();
			TerminatedAccountListModel model = (TerminatedAccountListModel)rows[0];
			models.add(model);
		}
		
		results.close();
		HibernateManager.endSession();
		
		return models;
	}
	
	/**
	 * 用時間跟數量讀取model
	 * @param time
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList<TerminatedAccountListModel> loadModels(long time, long size) {		
		Session session = HibernateManager.getSession();
		
		SQLQuery query = session.createSQLQuery("select * from terminated_account_list where startTime <= :TIME limit :SIZE");
		query.addEntity(TerminatedAccountListModel.class);
		query.setParameter("TIME", time);
		query.setParameter("SIZE", size);
		ArrayList<TerminatedAccountListModel> models = (ArrayList<TerminatedAccountListModel>) query.list();
		HibernateManager.endSession();
		
		return models;
	}
	
	/**
	 * 用頁數取資料
	 * @param Page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList<TerminatedAccountListModel> loadModelByPage(int numOfPage, int from) {
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("From TerminatedAccountListModel order by startTime desc");
		query.setFirstResult(from);
		query.setMaxResults(numOfPage);
		ArrayList<TerminatedAccountListModel> models = (ArrayList<TerminatedAccountListModel>) query.list();
		HibernateManager.endSession();
		return models;
	}
	
	/**
	 * 取得總筆數
	 * @return
	 */
	public static int getPageNum(int numOfPage) {
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("select count(*) from TerminatedAccountListModel");
		Long rows = (Long)query.uniqueResult();
		HibernateManager.endSession();
		
		int totalRow = rows.intValue();
		int totalPage =  totalRow / numOfPage;
		if(totalRow % numOfPage > 0){
			totalPage += 1;
		}
		
		return totalPage;
	}
}
